//
//  IndexConfigInterface.cpp
//  ChartRefactor
//
//  Created by CaoJun on 15/3/24.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#include "IndexConfigInterface.h"
#include "YTXChartTools.h"
#include "YTXConfig.h"

#import <Foundation/Foundation.h>

using namespace std;
using namespace ytx;

IndexConfigInterface::IndexConfigInterface( std::string indexId, LineType::LineType indexQuotationType) : indexQuotationType(indexQuotationType), indexId(indexId)
{

}

void IndexConfigInterface::clearAllIndexPermission()
{
    for (map<IndexType::IndexType, IndexConfig>::iterator it = indexConfigs.begin(); it != indexConfigs.end(); ++it) {
        (*it).second.clearIndexPermission();
    }
}
void IndexConfigInterface::addIndexPermission(IndexType::IndexType indexType, int indexPermission)
{
    if (indexConfigs.find(indexType) != indexConfigs.end()) {
        (*indexConfigs.find(indexType)).second.setIndexPermission(indexPermission);
    }
    
}

pair<bool, IndexConfig> IndexConfigInterface::haveParticularIndexAndHas()
{
    for (map<IndexType::IndexType, IndexConfig>::iterator it = indexConfigs.begin(); it != indexConfigs.end(); ++it) {
        if ((*it).second.isParticularAndHas()) {
            return make_pair(true, (*it).second);
        }
    }
    return make_pair(false, IndexConfig());
}

string IndexConfigInterface::IndexConfigUserDefaultKey = "QuotationIndexConfigUserDefault";

void IndexConfigInterface::setIndexConfig(map<IndexType::IndexType, IndexConfig> indexConfigs){
    this->indexConfigs = indexConfigs;
}

void IndexConfigInterface::setIndexConfig(IndexConfig indexConfig){
    map<IndexType::IndexType, IndexConfig>::iterator it = this->indexConfigs.find(indexConfig.indexType);
    it->second = indexConfig;
}

map<IndexType::IndexType, IndexConfig> IndexConfigInterface::getIndexConfigs()
{
    return indexConfigs;
}

IndexConfig & IndexConfigInterface::getIndexConfig(IndexType::IndexType type){
    map<IndexType::IndexType, IndexConfig>::iterator it = indexConfigs.find(type);
    
    return it->second;
}

string IndexConfigInterface::getUserDefaultKey(IndexType::IndexType type)
{
    string key = IndexConfigInterface::IndexConfigUserDefaultKey+"+"+indexId+"+"+LineType::convertLineTypeToString( indexQuotationType )+"+"+ConvertToString( (int)type );
    return key;
}

void IndexConfigInterface::saveIndexConfigToUserDefault(IndexType::IndexType indexType)
{
    
    map<IndexType::IndexType, IndexConfig>::iterator indexConfig_it = this->indexConfigs.find(indexType);
    if (indexConfig_it == indexConfigs.end()) {
        return;
    }
    
    IndexConfig & indexConfig = (*indexConfig_it).second;
    
    string json = indexConfig.toJSONString();
    
    NSString * key = [NSString stringWithUTF8String:getUserDefaultKey(indexConfig.indexType).c_str()];
    
    NSString * groupName = YTXConfig::getInstance()->userDefaultGroupName == "" ? nil : [NSString stringWithUTF8String:YTXConfig::getInstance()->userDefaultGroupName.c_str()];

    [[[NSUserDefaults alloc] initWithSuiteName:groupName] setObject:[NSString stringWithUTF8String:json.c_str()] forKey: key];
}

void IndexConfigInterface::clearIndexConfigToUserDefault(IndexType::IndexType type)
{
    NSString * groupName = YTXConfig::getInstance()->userDefaultGroupName == "" ? nil : [NSString stringWithUTF8String:YTXConfig::getInstance()->userDefaultGroupName.c_str()];
    [[[NSUserDefaults alloc] initWithSuiteName:groupName] setObject:@"" forKey: [NSString stringWithUTF8String:getUserDefaultKey(type).c_str()] ];
}

void IndexConfigInterface::resetIndexConfigToConfigDefault(IndexType::IndexType type)
{
    clearIndexConfigToUserDefault(type);
    
    indexConfigs.find(type)->second = YTXConfig::getInstance()->index.find(type)->second;
    
}


IndexConfig IndexConfigInterface::getIndexConfigToUserDefault(IndexType::IndexType type)
{
    NSString * groupName = YTXConfig::getInstance()->userDefaultGroupName == "" ? nil : [NSString stringWithUTF8String:YTXConfig::getInstance()->userDefaultGroupName.c_str()];
    std::string str =getUserDefaultKey(type);
    NSString * retNSString = [[[NSUserDefaults alloc] initWithSuiteName:groupName]  objectForKey:[NSString stringWithUTF8String:str.c_str()] ];
    string ret = "";
    if(retNSString){
        ret = [retNSString UTF8String];
    }
    if (ret == "") {
        //empty
        return IndexConfig();
    }
    IndexConfig config = IndexConfig::createByJSONString(ret);
    return config;
}

void IndexConfigInterface::setIndexConfigByUserDefaultOrGivenValue(map<IndexType::IndexType, IndexConfig> indexConfigs)
{
    for (map<IndexType::IndexType, IndexConfig>::iterator it =indexConfigs.begin(); it!= indexConfigs.end(); ++it) {
        IndexType::IndexType indexType = (*it).first;
        IndexConfig indexConfig = getIndexConfigToUserDefault( indexType );
        //如果没有Default就用默认传入的
        if (indexConfig.indexType == IndexType::EMPTY) {
            indexConfig = (*it).second;
        }
        
        //没找到直接设置
        map<IndexType::IndexType, IndexConfig>::iterator indexConfig_it = this->indexConfigs.find(indexType);
        if ( indexConfig_it == this->indexConfigs.end() )
        {
            this->indexConfigs.insert(make_pair(indexType, indexConfig));
        }
        else{
            indexConfig_it->second.extendSelf( indexConfig );
        }
        
    }
}

void IndexConfigInterface::loadIndexConfigByUserDefault()
{
    for (map<IndexType::IndexType, IndexConfig>::iterator it =indexConfigs.begin(); it!= indexConfigs.end(); ++it) {
        IndexType::IndexType indexType = (*it).first;
        
        IndexConfig indexConfig = getIndexConfigToUserDefault( indexType );
        
        if (indexConfig.indexType == IndexType::EMPTY) {
            continue;
        }
        
        map<IndexType::IndexType, IndexConfig>::iterator indexConfig_it = this->indexConfigs.find(indexType);
        if ( indexConfig_it == this->indexConfigs.end() )
        {
            this->indexConfigs.insert(make_pair( indexType, indexConfig) );
        }
        else{
            indexConfig_it->second.extendSelf( indexConfig );
        }
    }
}
